//package com.qujia.station.task;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.qujia.common.core.controller.BaseController;
//import com.qujia.common.core.redis.RedisCache;
//import com.qujia.common.utils.DateUtils;
//import com.qujia.label.domain.LabelRule;
//import com.qujia.label.domain.TimingLabel;
//import com.qujia.label.service.ILabelRuleService;
//import com.qujia.label.service.ITimingLabelService;
//import com.qujia.station.domain.*;
//import com.qujia.station.domain.vo.FinanceStatement;
//import com.qujia.station.service.*;
//
//import com.qujia.station.service.IPayStationBetweenService;
//import com.qujia.station.service.IStationOillGunService;
//import com.qujia.station.service.ITimingPayWayService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import java.math.BigDecimal;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.util.ArrayList;
//import java.util.Calendar;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
//public class SaticScheduleTask {
//    @Autowired
//    private IPayOrderService payOrderService;
//    @Autowired
//    private ILabelRuleService labelRuleService;
//    @Autowired
//    private ITimingLabelService timingLabelService;
//
//
//    @Autowired
//    private ITimingPayWayService timingPayWayService;
//
//    @Autowired
//    private IPayStationBetweenService payStationBetweenService;
//
//    @Autowired
//    private IStationOillGunService stationOillGunService;
//
//    @Autowired
//    private IStationService stationService;
//    @Autowired
//    private IEnterpriseService enterpriseService;
//    @Autowired
//    private IFinanceStatementService financeStatementService;
//    @Autowired
//    private IFinanceStatementStationService financeStatementStationService;
//    @Autowired
//    private IFinanceStatementEnterpriseService financeStatementEnterpriseService;
//
//    @Autowired
//    private RedisCache redisCache;
//    @Autowired
//    public RedisTemplate redisTemplate;
//    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
//
//    //3.添加定时任务
//    // @Scheduled(cron = "0/5 * * * * ?")
//    //或直接指定时间间隔，例如：5秒 5000
//    @Scheduled(fixedRate=86400000)
//    private void configureTasks() throws ParseException {
//        String newdate = DateUtils.getTime();
//        System.err.println("执行静态定时任务时间: " + newdate);
//        logger.info("执行静态定时任务时间: " + newdate);
//        try {
//            payOrderService.chengePayOrder();
//        } catch (Exception e) {
//            logger.info("定时任务报错 " + newdate);
//        }
//        logger.info("结束定时任务 " + newdate);
//    }
//
//
//
//
//    //3.添加定时任务
//    // @Scheduled(cron = "0/5 * * * * ?")
//    //或直接指定时间间隔，例如：5秒 5000
//    @Scheduled(fixedRate=60000)
//    private void LabelTasks() throws ParseException {
//        String newdate = DateUtils.getTime();
//        System.err.println("执行定时标签规则 " + newdate);
//        logger.info("开始执行订单标签规则: " + newdate);
//        TimingLabel timingLabel=new TimingLabel();
//        timingLabel.setDelFlag("0");
//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//        SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd");
//        LabelRule labelRule=new LabelRule();
//        //计算当前是周几以及几号，查询时间是否符合
//        Calendar calendar = Calendar.getInstance();
//        int week = calendar.get(Calendar.DAY_OF_WEEK);
//        int month = calendar.get(Calendar.DAY_OF_MONTH);
//        if(week == 1){
//            week = 7;
//        }else{
//            week--;
//        }
//        timingLabel.setWeek(week+"");
//        timingLabel.setMonth(month+"");
//        LabelRule labelRule1=new LabelRule();
//        //筛选出所有的执行时间
//        List<TimingLabel> timingLabels=timingLabelService.selectTimingLabelList(timingLabel);
//        boolean updateFlag=false;
//        if(timingLabels!=null&&timingLabels.size()>0){
//            for(TimingLabel timingLabel1:timingLabels){
//
//                // 结束时间小于开始时间
//                if(timingLabel1.getBeginDate().toString().compareTo(timingLabel1.getEndDate().toString())>=0){
//                    //当前时间大于开始时间，小于最大时间
//                    if(sdf.format(new Date()).compareTo(timingLabel1.getBeginDate().toString())>=0&&sdf.format(new Date()).compareTo("23:59:59")<=0){
//                        if(timingLabel1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingLabel1.getLastUpdateDate()).compareTo(timingLabel1.getBeginDate().toString())>=0&&sdf.format(timingLabel1.getLastUpdateDate()).compareTo("23:59:59")<=0&&sdfday.format(timingLabel1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//                    }
//                    //当前时间大于最小时间   小于结束时间
//                    if(sdf.format(new Date()).compareTo("00:00:00")>=0&&sdf.format(new Date()).compareTo(timingLabel1.getEndDate().toString())<=0){
//                        if(timingLabel1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingLabel1.getLastUpdateDate()).compareTo("00:00:00")>=0&&sdfday.format(timingLabel1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0&&sdf.format(timingLabel1.getLastUpdateDate()).compareTo(timingLabel1.getEndDate().toString())<=0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//
//                    }
//                }
//                //结束时间大于开始时间
//                else {
//                    //当前时间大于开始时间，小于结束时间,满足条件进行更新
//                    if (sdf.format(new Date()).compareTo(timingLabel1.getBeginDate().toString()) >= 0 && sdf.format(new Date()).compareTo(timingLabel1.getEndDate().toString()) <= 0) {
//                        if(timingLabel1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingLabel1.getLastUpdateDate()).compareTo(timingLabel1.getBeginDate().toString())>=0&&sdf.format(timingLabel1.getLastUpdateDate()).compareTo(timingLabel1.getEndDate().toString())<=0&&sdfday.format(timingLabel1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//                    }
//                }
//                if(updateFlag){
//                    labelRule.setId(Math.toIntExact(timingLabel1.getLabelId()));
//                    labelRule.setActivityIds(timingLabel1.getActivityId()+"");
//                    labelRule.setEnterpriseId(timingLabel1.getEnterpriseId());
//                    labelRule.setUpdateTime(new Date());
//                    timingLabel1.setLastUpdateDate(new Date());
//                    timingLabelService.updateTimingLabel(timingLabel1);
//                    labelRuleService.updateLabelRule(labelRule);
//                    System.out.println("更新完成，更新的定时标签，油企是"+labelRule.getEnterpriseId()+" 标签是 "+labelRule.getId()+"开始时间"+timingLabel1.getBeginDate()+"结束时间"+timingLabel1.getEndDate()+"立减活动"+timingLabel1.getActivityId());
//                    updateFlag=false;
//
//                }
//            }
//        }
//        logger.info("定时修改标签完成 " + newdate);
//
//    }
//
//
//    //3.添加定时任务
//    // @Scheduled(cron = "0/5 * * * * ?")
//    //或直接指定时间间隔，例如：5秒 5000
//    @Scheduled(fixedRate=60000)
//    private void payWayTasks() throws ParseException {
//        String newdate = DateUtils.getTime();
//        System.err.println("执行定时标签规则 " + newdate);
//        logger.info("开始执行订单标签规则: " + newdate);
//        TimingPayWay timingPayWay=new TimingPayWay();
//        timingPayWay.setDelFlag("0");
//        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//        SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd");
//        LabelRule labelRule=new LabelRule();
//        //计算当前是周几以及几号，查询时间是否符合
//        Calendar calendar = Calendar.getInstance();
//        int week = calendar.get(Calendar.DAY_OF_WEEK);
//        int month = calendar.get(Calendar.DAY_OF_MONTH);
//        if(week == 1){
//            week = 7;
//        }else{
//            week--;
//        }
//        timingPayWay.setWeek(week+"");
//        timingPayWay.setMonth(month+"");
//        LabelRule labelRule1=new LabelRule();
//        //筛选出所有的执行时间
//        List<TimingPayWay> timingPayWays=timingPayWayService.selectTimingPayWayList(timingPayWay);
//        boolean updateFlag=false;
//        if(timingPayWays!=null&&timingPayWays.size()>0){
//            for(TimingPayWay timingPayWay1:timingPayWays){
//
//                // 结束时间小于开始时间
//                if(timingPayWay1.getBeginDate().toString().compareTo(timingPayWay1.getEndDate().toString())>=0){
//                    //当前时间大于开始时间，小于最大时间
//                    if(sdf.format(new Date()).compareTo(timingPayWay1.getBeginDate().toString())>=0&&sdf.format(new Date()).compareTo("23:59:59")<=0){
//                        if(timingPayWay1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingPayWay1.getLastUpdateDate()).compareTo(timingPayWay1.getBeginDate().toString())>=0&&sdf.format(timingPayWay1.getLastUpdateDate()).compareTo("23:59:59")<=0&&sdfday.format(timingPayWay1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//                    }
//                    //当前时间大于最小时间   小于结束时间
//                    if(sdf.format(new Date()).compareTo("00:00:00")>=0&&sdf.format(new Date()).compareTo(timingPayWay1.getEndDate().toString())<=0){
//                        if(timingPayWay1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingPayWay1.getLastUpdateDate()).compareTo("00:00:00")>=0&&sdf.format(timingPayWay1.getLastUpdateDate()).compareTo(timingPayWay1.getEndDate().toString())<=0&&sdfday.format(timingPayWay1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//
//                    }
//                }
//                //开始时间大于结束时间
//                else {
//                    //当前时间大于开始时间，小于开始时间,满足条件进行更新
//                    if (sdf.format(new Date()).compareTo(timingPayWay1.getBeginDate().toString()) >= 0 && sdf.format(new Date()).compareTo(timingPayWay1.getEndDate().toString()) <= 0) {
//                        if(timingPayWay1.getLastUpdateDate()==null) {
//                            updateFlag = true;
//                        }else if(sdf.format(timingPayWay1.getLastUpdateDate()).compareTo(timingPayWay1.getBeginDate().toString())>=0&&sdf.format(timingPayWay1.getLastUpdateDate()).compareTo(timingPayWay1.getEndDate().toString())<=0&&sdfday.format(timingPayWay1.getLastUpdateDate()).compareTo(sdfday.format(new Date()))==0){
//                            updateFlag = false;
//                        }else {
//                            updateFlag = true;
//                        }
//                    }
//                }
//                if(updateFlag){
//
//
//                    List<PayWayStationBetween> payWayStationBetweens=new ArrayList<>();
//
//                    PayWayStationBetween  payWayStationBetweenwx = new  PayWayStationBetween();
//                    payWayStationBetweenwx.setPayWayId(timingPayWay1.getWxPayWayId());
//                    payWayStationBetweenwx.setStationId(timingPayWay1.getStationId());
//                    payWayStationBetweens.add(payWayStationBetweenwx);
//
//                    PayWayStationBetween  payWayStationBetweenwxRecharge = new  PayWayStationBetween();
//                    payWayStationBetweenwxRecharge.setPayWayId(timingPayWay1.getWxRechargePayWayId());
//                    payWayStationBetweenwxRecharge.setStationId(timingPayWay1.getStationId());
//                    payWayStationBetweens.add(payWayStationBetweenwxRecharge);
//
//                    PayWayStationBetween  payWayStationBetweenzfb = new  PayWayStationBetween();
//                    payWayStationBetweenzfb.setPayWayId(timingPayWay1.getZfbPayWayId());
//                    payWayStationBetweenzfb.setStationId(timingPayWay1.getStationId());
//                    payWayStationBetweens.add(payWayStationBetweenzfb);
//
//                    timingPayWay1.setLastUpdateDate(new Date());
//                    timingPayWayService.updateTimingPayWay(timingPayWay1);
//                    try {
//                        payStationBetweenService.timingAddPayWayStationBetweenList(payWayStationBetweens,timingPayWay1.getStationId());
//                    } catch (Exception e) {
//                        System.out.println("定时更新关联表的信息失败 "+e.getMessage());
//                    }
//                    System.out.println("定时更新油站信息支付方式成功");
//                    updateFlag=false;
//
//                }
//            }
//        }
//        logger.info("定时更新油站信息支付方式成功 " + newdate);
//
//    }
////一个小时
////    @Scheduled(fixedRate=3600000)
//    @Scheduled(cron = "0 0 0/1 * * ?")
//    public void stationOillGunTask() throws ParseException{
//        try {
//            Date now = new Date();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
//            Boolean hasKey = redisTemplate.hasKey("stationOillGuntoTemp");
//            List<StationOillGun> stationOillGunList = new ArrayList();
//            if(!hasKey){
//                stationOillGunList = stationOillGunService.queryStationOillGunforEffectDate();
//                redisCache.setCacheObject("stationOillGuntoTemp",JSON.toJSONString(stationOillGunList));
//            }else{
//                stationOillGunList=JSONArray.parseArray(redisCache.getCacheObject("stationOillGuntoTemp"),StationOillGun.class);
//            }
//            if(stationOillGunList!=null&&stationOillGunList.size()>0) {
//                for (StationOillGun stationOillGun : stationOillGunList) {
//                    if (sdf.format(stationOillGun.getEffectDate()).compareTo(sdf.format(now)) <= 0&&!"Y".equals(stationOillGun.getUpdatePriceFlag())) {
//                        stationOillGun.setOilStationSettlementPrice(stationOillGun.getOilStationSettlementPrice()==null?BigDecimal.ZERO:stationOillGun.getOilStationSettlementPrice());
//                        stationOillGun.setDiscountPrice(stationOillGun.getDiscountPrice()==null? BigDecimal.ZERO:stationOillGun.getDiscountPrice());
//                        stationOillGun.setOilStationPrice(stationOillGun.getOilStationPrice()==null?BigDecimal.ZERO:stationOillGun.getOilStationPrice());
//                        stationOillGun.setOilOfficialPrice(stationOillGun.getOilOfficialPrice()==null?BigDecimal.ZERO:stationOillGun.getOilOfficialPrice());
//                        stationOillGunService.updateStationOillGun(stationOillGun);
//                        stationOillGun.setUpdatePriceFlag("Y");
//                        stationOillGunService.updateStationOillGuntoTemp(stationOillGun);
//                        List<StationOillGun> collect = JSONArray.parseArray(JSON.toJSONString(stationOillGunList), StationOillGun.class);
//                        collect = collect.stream().filter(s -> !s.getGunId().equals(stationOillGun.getGunId())).collect(Collectors.toList());
//                        redisCache.setCacheObject("stationOillGuntoTemp",JSON.toJSONString(collect));
//                        logger.info("定时更新价格成功 ,生效时间为" + stationOillGun.getEffectDate());
//                    }else{
//                        //System.out.println("生效时间还没到");
//                    }
//                }
//            }else{
//                //System.out.println("暂无可更新的数据");
//            }
//        }catch(Exception e){
//            logger.info("定时更新价格失败 "+e.getMessage());
//        }
//    }
//
//    //@Scheduled(cron = "0 0 0 * * *")
//    public void runFinanceStatementTask(){
//        logger.info("财务报表任务===开始");
//        try {
//            LocalDate yesterday = LocalDate.now().minusDays(1L);
//            runCountStationTask(yesterday);
//            runCountEnterpriseTask(yesterday);
//        }catch (Exception e){
//            logger.error("财务报表任务===错误",e);
//        }
//        logger.info("财务报表任务===结束");
//    }
//
//    public void runCountStationTask(LocalDate date){
//        Station form = new Station();
//        form.setDeleteStatus("0");
//        List<Station> stations = stationService.selectStationList(form);
//        LocalDateTime countTime = LocalDateTime.of(date,LocalTime.of(0,0,0));
//        LocalDateTime endTime = LocalDateTime.of(date,LocalTime.of(23,59,59));
//        for (Station station : stations) {
//            FinanceStatement statement = financeStatementService.countStationStatement(station.getStationId(), countTime,endTime);
//            statement.setEnterpriseId(station.getEnterpriseId());
//            statement.setCountDate(countTime);
//            financeStatementStationService.save(FinanceStatementStation.of(statement));
//        }
//    }
//
//    public void runCountEnterpriseTask(LocalDate date){
//        Enterprise form = new Enterprise();
//        form.setDeleteStatus("0");
//        List<Enterprise> enterprises = enterpriseService.selectEnterpriseList(form);
//        LocalDateTime countTime = LocalDateTime.of(date, LocalTime.of(0,0,0));
//        LocalDateTime endTime = LocalDateTime.of(date,LocalTime.of(23,59,59));
//        for (Enterprise enterprise : enterprises) {
//            FinanceStatement statement = financeStatementService.countEnterpriseStatement(enterprise.getEnterpriseId(), countTime,endTime);
//            statement = statement == null ? new FinanceStatement() : statement;
//            statement.setCountDate(countTime);
//            statement.setEnterpriseId(enterprise.getEnterpriseId());
//            financeStatementEnterpriseService.save(FinanceStatementEnterprise.of(statement));
//        }
//    }
//
//}
